import sys
import random
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QLabel,
                             QPushButton, QSlider, QGridLayout, QHBoxLayout,
                             QVBoxLayout, QGroupBox)
from PyQt5.QtCore import Qt, QTimer

class DeviceWidget(QWidget):
    def __init__(self, title, device_type, parent=None):
        super().__init__(parent)
        self.device_type = device_type
        self.status = False
        self.value = 0
        
        layout = QVBoxLayout()
        
        # 设备标题
        self.title_label = QLabel(title)
        self.title_label.setStyleSheet("font-weight: bold; font-size: 16px;")
        layout.addWidget(self.title_label)
        
        # 状态显示
        self.status_label = QLabel("状态: 关闭")
        self.status_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.status_label)
        
        # 控制部件（根据设备类型不同）
        self.init_control()
        
        self.setLayout(layout)

    def init_control(self):
        pass  # 由子类实现

    def update_status(self):
        pass  # 由子类实现

class LightWidget(DeviceWidget):
    def init_control(self):
        self.button = QPushButton("切换开关")
        self.button.clicked.connect(self.toggle)
        self.layout().addWidget(self.button)

    def toggle(self):
        self.status = not self.status
        self.update_status()

    def update_status(self):
        state = "开启" if self.status else "关闭"
        color = "green" if self.status else "red"
        self.status_label.setText(f"状态: {state}")
        self.status_label.setStyleSheet(f"color: {color}; font-weight: bold;")

class ThermostatWidget(DeviceWidget):
    def init_control(self):
        # 温度显示
        self.temp_label = QLabel("25°C")
        self.temp_label.setAlignment(Qt.AlignCenter)
        self.layout().addWidget(self.temp_label)
        
        # 滑块控制
        self.slider = QSlider(Qt.Horizontal)
        self.slider.setRange(16, 30)
        self.slider.setValue(25)
        self.slider.valueChanged.connect(self.set_temperature)
        self.layout().addWidget(self.slider)

    def set_temperature(self, value):
        self.value = value
        self.temp_label.setText(f"{value}°C")
        self.temp_label.setStyleSheet("font-weight: bold;")

class SecurityWidget(DeviceWidget):
    def init_control(self):
        self.lock_button = QPushButton("锁定门锁")
        self.lock_button.clicked.connect(self.toggle_lock)
        self.layout().addWidget(self.lock_button)

    def toggle_lock(self):
        self.status = not self.status
        state = "已锁定" if self.status else "已解锁"
        self.lock_button.setText(state)
        self.lock_button.setStyleSheet(
            f"background-color: {'green' if self.status else 'red'};"
        )

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("智能家居控制系统")
        self.setGeometry(100, 100, 800, 500)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        main_layout = QHBoxLayout(central_widget)
        
        # 左侧设备面板
        left_panel = QVBoxLayout()
        
        # 灯光控制
        light_group = QGroupBox("灯光控制")
        light_layout = QVBoxLayout()
        self.light = LightWidget("客厅灯", "light")
        light_layout.addWidget(self.light)
        light_group.setLayout(light_layout)
        left_panel.addWidget(light_group)
        
        # 温度控制
        thermostat_group = QGroupBox("温控系统")
        thermostat_layout = QVBoxLayout()
        self.thermostat = ThermostatWidget("客厅温度", "thermostat")
        thermostat_layout.addWidget(self.thermostat)
        thermostat_group.setLayout(thermostat_layout)
        left_panel.addWidget(thermostat_group)
        
        main_layout.addLayout(left_panel, 1)
        
        # 右侧状态面板
        right_panel = QVBoxLayout()
        
        # 安全系统
        security_group = QGroupBox("安防系统")
        security_layout = QVBoxLayout()
        self.security = SecurityWidget("前门锁", "security")
        security_layout.addWidget(self.security)
        security_group.setLayout(security_layout)
        right_panel.addWidget(security_group)
        
        # 环境传感器
        sensor_group = QGroupBox("环境监测")
        sensor_layout = QVBoxLayout()
        self.sensor = EnvironmentWidget()
        sensor_layout.addWidget(self.sensor)
        sensor_group.setLayout(sensor_layout)
        right_panel.addWidget(sensor_group)
        
        main_layout.addLayout(right_panel, 1)
        
        # 定时器用于模拟数据更新
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_data)
        self.timer.start(3000)  # 每3秒更新一次

    def update_data(self):
        # 模拟环境数据更新
        humidity = random.randint(30, 70)
        temperature = random.randint(20, 30)
        status = "正常" if random.random() > 0.2 else "异常"
        
        self.sensor.update_display(temperature, humidity, status)

class EnvironmentWidget(DeviceWidget):
    def init_control(self):
        layout = QVBoxLayout()
        
        # 温度显示
        self.temp_label = QLabel("25°C")
        self.temp_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.temp_label)
        
        # 湿度显示
        self.humid_label = QLabel("50%")
        self.humid_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.humid_label)
        
        # 状态指示
        self.status_led = QLabel()
        self.status_led.setFixedSize(20, 20)
        self.status_led.setStyleSheet("border-radius: 10px;")
        layout.addWidget(self.status_led)
        
        self.layout().addLayout(layout)

    def update_display(self, temp, humid, status):
        self.temp_label.setText(f"{temp}°C")
        self.humid_label.setText(f"{humid}%")
        
        color = "green" if status == "正常" else "red"
        self.status_led.setStyleSheet(f"background-color: {color}; border-radius: 10px;")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())